/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspSlice;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.Slice;
import com.ffcs.crmd.tsp.server.util.PublicMethodHelper;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QuerySliceProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 任务数据库服务
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QuerySliceProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query slice is received that body=[{}]", requestXml);
            TspSlice tspSliceVO = (TspSlice) XmlUtil.fromXml(requestXml, TspSlice.class);
            RetVo<TspSlice> tspSliceRetVo = new RetVo<TspSlice>();
            
            Slice sliceParam = new Slice();
            BeanUtils.applyIf(sliceParam, tspSliceVO);
            
            if (PublicMethodHelper.isQueryPage(tspSliceVO.getPageNumber(), tspSliceVO.getPageSize())) {
                PageInfo<Slice> slicePageInfo = dataBaseHandler.getServiceSupport().querySlicePage(sliceParam);
                PageInfo<TspSlice> tspSlicePageInfo = new PageInfo<TspSlice>();
                List<TspSlice> tspSliceList = BeanUtils.copyList(slicePageInfo.getList(), TspSlice.class);
                BeanUtils.applyIf(tspSlicePageInfo, slicePageInfo);
                tspSlicePageInfo.setList(tspSliceList);
                tspSliceRetVo.setPageInfo(tspSlicePageInfo);
            } else if (tspSliceVO.getId() != null) {
                TspSlice tspSlice = new TspSlice();
                Slice slice = dataBaseHandler.getServiceSupport().getSlice(sliceParam);
                BeanUtils.applyIf(tspSlice, slice);
                tspSliceRetVo.setObject(tspSlice);
            } else {
                List<Slice> sliceList = dataBaseHandler.getServiceSupport().querySlice(sliceParam);
                List<TspSlice> tspSliceList = BeanUtils.copyList(sliceList, TspSlice.class);
                tspSliceRetVo.setDataList(tspSliceList);
            }
            response.setBody(XmlUtil.toXml(tspSliceRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query slice[{}] exception:", requestXml, e);
            String responseBody = String.format("query slice:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
